namespace ScrumSprintMonitor.ConfigEditor.Views.ConfigurationView
{
	#region #using Directives

	using System;
	using System.Diagnostics;
	using System.Windows;
	using System.Windows.Input;

	using Microsoft.Practices.ServiceLocation;

	#endregion

	/// <summary>
	///   Interaction logic for ConfigurationWindow.xaml
	/// </summary>
	public partial class ConfigurationWindow : Window
	{
		private readonly IConfigurationViewModel model;

		public ConfigurationWindow()
		{
			InitializeComponent();
		}

		public ConfigurationWindow(IConfigurationViewModel model)
		{
			this.model = model;

			XamlServiceLocator.Assign(model);

			InitializeComponent();
		}

		public IConfigurationViewModel Model
		{
			get
			{
				if (this.model != null)
				{
					return this.model;
				}

				return ServiceLocator.Current.GetInstance<IConfigurationViewModel>();
			}
		}

		private void OnWindowLoaded(object sender, RoutedEventArgs e)
		{
			this.tfsServerNameComboBox.Focus();
		}

		private void OnWindowActivated(object sender, EventArgs e)
		{
			this.model.OnViewActivated();
		}

		private void OnWindowMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			DragMove();
		}

		private void OnHyperlinkRequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
		{
			Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
			e.Handled = true;
		}

		private void OnDonateButtonMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			VisitDonatePage();
			e.Handled = true;
		}

		private void OnDonateButtonKeyDown(object sender, KeyEventArgs e)
		{
			if (e.Key == Key.Space)
			{
				VisitDonatePage();
				e.Handled = true;
			}
		}

		private static void VisitDonatePage()
		{
			Process.Start(new ProcessStartInfo(@"https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=Q23X5YZQ29NDC&lc=PT&item_name=Scrum%20Sprint%20Monitor&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donate_LG%2egif%3aNonHosted"));
		}
	}
}